<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-form>
        <el-form-item>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-input placeholder="请输入内容" v-model="query.query">
                <el-button slot="append" icon="el-icon-search" @click="list()"></el-button>
              </el-input>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" @click="add">添加商品</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>

      <el-table :data="data" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50px"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="599px">
        </el-table-column>
        <el-table-column prop="hot_mumber" label="商品价格(元)" width="100px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100px">
        </el-table-column>
        <el-table-column prop="add_time" label="添加时间" width="150px">
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="{ row:{goods_id}}">
          <el-button type="primary" size="small" @click="edit(goods_id)"
            ><i class="el-icon-edit"></i>编辑</el-button
          >
          <el-button type="danger" size="small"
          @click="del(goods_id)"
            ><i class="el-icon-delete"></i>删除</el-button
          >
        </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
  <script>
import { del } from 'vue';

export default {
  data() {
    return {
      query: {
        //查询
        query: "",
        //每页几条
        pagesize: 5,
        //当前第几页
        pagenum: 1,
      },
      total: 5,
      data: [],
    };
  },
  methods: {
    handleSizeChange(val) {
      this.query.pagesize = val;
      this.list();
    },
    handleCurrentChange(val) {
      this.query.pagenum = val;
      this.list();
    },
    edit(id) {
      this.$router.push(`/edit/${id
      }`);
    },
    add(){
      this.$router.push("/add");
    },
    async list(){
        let {data:{data:{goods,pagenum,total}}} = await this.$http.get(
            "goods",{params:this.query}
        )
        this.data = goods
        this.total= total
    },
    async del(id){
       const {data:{data,meta}} = await this.$http.delete(`goods/${id}`);
       if(meta.status!=200)return this.$message.error("删除商品失败");
      this.$message.success("删除商品成功");
      this.list()
    }
  },
  created(){
    this.list()
  }
};
</script>